import { _decorator, Component, Graphics, Label, Node, Sprite, UIOpacity } from 'cc';
import { GameSticker, GameStickerType } from '../../Model/GameStickerMgr';
import { UnitPlanetBase } from '../UnitPlanetBase';
import { EconomicCom } from '../Component/EconomicCom';
import { DestructableCom } from '../Component/DestructableCom';
import { FactoryCom } from '../Component/FactoryCom';

const { ccclass, property } = _decorator;

@ccclass('PlanetEconomicSticker')
export class PlanetEconomicSticker extends GameSticker {

    type: GameStickerType = GameStickerType.PlanetEconomicSticker;
    target: UnitPlanetBase = null;
    setTarget(target: any) {
        this.target = target as UnitPlanetBase;
        this.lifeTime = 5;
    }
    economicCom: EconomicCom = null;
    destructableCom: DestructableCom = null;
    lifeTime: number = 0;

    @property(Sprite)
    electric_fill: Sprite = null;
    @property(Sprite)
    shield_fill: Sprite = null;
    @property(Label)
    number_label: Label = null;
    @property(UIOpacity)
    selfOpacity: UIOpacity = null;

    update(dt: number) {
        this.lifeTime -= dt;
        if (!this.target || !this.target.isValid || this.lifeTime <= 0) {
            // PoolManager.putNode('PlanetEconomicSticker', this.node);
            this.node.active = false;
            this.target = null;
        }
        else {
            this.node.setWorldPosition(this.target.unitWorldPos);
            this.RefreshContentUI();
        }
    }

    protected onDisable(): void {
        this.target = null;
        this.lifeTime = 0;
        this.economicCom = null;
        this.destructableCom = null;
    }

    RefreshContentUI() {
        if (this.target == null) return;
        this.economicCom = this.target.getComponent(EconomicCom);
        if (this.economicCom == null || !this.economicCom.isValid) return;
        let factory = this.target.getComponent(FactoryCom);
        if (factory == null || !factory.isValid) return;

        let ratio = this.economicCom.electric_power_remain / this.economicCom.electric_power_max;
        this.number_label.string = (ratio * 100).toFixed(0) + '%';
        ratio = -ratio * 0.61;
        this.electric_fill.fillRange = ratio;

        this.destructableCom = this.target.getComponent(DestructableCom);
        if (this.destructableCom && this.destructableCom.maxShield > 0) {
            ratio = -this.destructableCom.shield / this.destructableCom.maxShield;
            this.shield_fill.fillRange = ratio;
        }
        else {
            this.shield_fill.fillRange = 0;
        }

        // this.number_label.string = (this.economicCom.power_load * 100).toFixed(0) + '%';
    }
}


